package com.bp.system.service.impl;

import com.bp.common.constant.UserConstants;
import com.bp.common.core.domain.TreeSelect;
import com.bp.common.exception.BusinessException;
import com.bp.common.utils.DateUtils;
import com.bp.common.utils.StringUtils;
import com.bp.system.domain.SysFileIndex;
import com.bp.system.mapper.SysFileIndexMapper;
import com.bp.system.service.ISysFileIndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户目录Service业务层处理
 *
 * @author shaominjie
 * @date 2021-01-13
 */
@Service
public class SysFileIndexServiceImpl implements ISysFileIndexService {
    @Resource
    private SysFileIndexMapper sysFileIndexMapper;

    /**
     * 查询用户目录
     *
     * @param indexId 用户目录ID
     * @return 用户目录
     */
    @Override
    public SysFileIndex selectById(Long indexId) {
        return sysFileIndexMapper.selectById(indexId);
    }

    /**
     * 查询用户目录列表
     *
     * @param sysFileIndex 用户目录
     * @return 用户目录
     */
    @Override
    public List<SysFileIndex> selectList(SysFileIndex sysFileIndex) {
        return sysFileIndexMapper.selectList(sysFileIndex);
    }

    /**
     * 新增用户目录
     *
     * @param sysFileIndex 用户目录
     * @return 结果
     */
    @Override
    public int insert(SysFileIndex sysFileIndex) {
        if(sysFileIndex.getParentId() != null){
            SysFileIndex info = sysFileIndexMapper.selectById(sysFileIndex.getParentId());
            // 如果父节点不为"正常"状态,则不允许新增子节点
            if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
                throw new BusinessException("上级目录停用，不允许新增");
            }
            sysFileIndex.setAncestors(info.getAncestors() + "," + sysFileIndex.getParentId());
        }else{
            sysFileIndex.setParentId(0L);
            sysFileIndex.setAncestors("0");
        }
        sysFileIndex.setCreateTime(DateUtils.getNowDate());
        return sysFileIndexMapper.insert(sysFileIndex);
    }

    /**
     * 修改用户目录
     *
     * @param sysFileIndex 用户目录
     * @return 结果
     */
    @Override
    public int update(SysFileIndex sysFileIndex) {
        sysFileIndex.setUpdateTime(DateUtils.getNowDate());
        return sysFileIndexMapper.update(sysFileIndex);
    }

    /**
     * 批量删除用户目录
     *
     * @param indexIds 需要删除的用户目录ID
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] indexIds) {
        return sysFileIndexMapper.deleteByIds(indexIds);
    }

    /**
     * 删除用户目录信息
     *
     * @param indexId 用户目录ID
     * @return 结果
     */
    @Override
    public int deleteById(Long indexId) {
        return sysFileIndexMapper.deleteById(indexId);
    }

    /**
     * 查询用户目录列表
     *
     * @param userId 用户id
     * @return 用户目录集合
     */
    @Override
    public List<SysFileIndex> selectByUserId(Long userId) {
        return sysFileIndexMapper.selectByUserId(userId);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param indexList 列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect<Long>> buildTree(List<SysFileIndex> indexList) {
        List<SysFileIndex> returnList = new ArrayList<>();
        List<Long> tempList = new ArrayList<>();
        for (SysFileIndex tmp : indexList) {
            tempList.add(tmp.getIndexId());
        }
        for (SysFileIndex tmp : indexList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(tmp.getParentId())) {
                recursionFn(indexList, tmp);
                returnList.add(tmp);
            }
        }
        if (returnList.isEmpty()) {
            returnList = indexList;
        }

        return returnList.stream().map(this::tree).collect(Collectors.toList());
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<SysFileIndex> list, SysFileIndex t) {
        // 得到子节点列表
        List<SysFileIndex> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysFileIndex tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<SysFileIndex> it = childList.iterator();
                while (it.hasNext()) {
                    SysFileIndex n =  it.next();
                    recursionFn(list, n);
                }
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<SysFileIndex> getChildList(List<SysFileIndex> list, SysFileIndex t) {
        List<SysFileIndex> tlist = new ArrayList<>();
        for (SysFileIndex n : list) {
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getIndexId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysFileIndex> list, SysFileIndex t) {
        return getChildList(list, t).size() > 0;
    }
    private TreeSelect<Long> tree(SysFileIndex index){
        TreeSelect<Long> deptTree = new TreeSelect<>();
        deptTree.setId(index.getIndexId());
        deptTree.setLabel(index.getIndexName());
        deptTree.setChildren(index.getChildren().stream().map(this::tree).collect(Collectors.toList()));
        return deptTree;
    }

    /**
     * 根据父级Id获取子集集合
     *
     * @param parentId 父级id
     * @return
     */
    @Override
    public List<SysFileIndex> selectByParentId(Long parentId) {
        return sysFileIndexMapper.selectByParentId(parentId);
    }
}